package org.dgmwebTest.hg.download;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;

public class PoiCustomComm {
	/**
	 * 
	 * genExcel
	 *
	 * @param head 头
	 * @param content 内容
	 * @Exception
	 * @author：denggm@asiainfo-linkage.com
	 * @Oct 10, 2012 3:16:33 PM 
	 * @update:
	 * @Oct 10, 2012 3:16:33 PM
	 */
	public HSSFWorkbook getExcelWorkBook(List head,List allContentList) throws Exception{
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		// create a new sheet
		HSSFSheet sheet = workbook.createSheet("导出工作表");

		// Define a few rows
		
		//填充头
		HSSFRow headRow = sheet.createRow(0);
		for (int j = 0; j < head.size(); j++) { //填列
			String headColumn = (String)head.get(j);
			HSSFCell headCell = headRow.createCell(j);
			headCell.setCellValue(headColumn);
		}
		
		//填充content
		for (int j = 0; j < allContentList.size(); j++) { //填行
			HSSFRow contentRow = sheet.createRow(j+1);
			List contentRowList = (List)allContentList.get(j); //单行的内容
			for (int k = 0; k < contentRowList.size(); k++) { //填列
				String contentColumn = (String)contentRowList.get(k);
				HSSFCell contentCell = contentRow.createCell(k);
				contentCell.setCellValue(contentColumn);
			}
		}

		// Save

		return workbook;
	}
	
	
	
	
	
//	@Test
	public void testgetExcel() throws Exception{
		List head = new ArrayList<String>();
		head.add("头一");
		head.add("头二");
		head.add("头三");
		
		List content1 = new ArrayList<String>();
		content1.add("content..");
		content1.add("content..");
		content1.add("content..");
		
		List content2 = new ArrayList<String>();
		content2.add("content..");
		content2.add("content..");
		content2.add("content..");
		
		
		List allContentList = new ArrayList<String>();
		allContentList.add(content1);
		allContentList.add(content2);
		
		
		HSSFWorkbook workbook = getExcelWorkBook(head,allContentList);
		String fileString = "f:/workbook1.xls";
		File file = new File(fileString);
		FileOutputStream out = new FileOutputStream(file);
		workbook.write(out);
		out.close();
		
	}
	
	
	
}
